package boun.swe.bob.service;

import boun.swe.bob.entities.Friend;
import boun.swe.bob.entities.FriendsPK;
import boun.swe.bob.entities.User;
import boun.swe.bob.entities.UserAction;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * This controller is used for profile page actions
 */
public class ProfilePageController extends BaseController {

    /**
     * Shows the page
     *
     * @param httpServletRequest http request
     * @param httpServletResponse http response
     * @return page
     */
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView showPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        Map<String, Object> myModel = Maps.newHashMap();
        String wantedUserId = httpServletRequest.getParameter("wantedUserId");
        User wantedUser = null;

        
        if(!Strings.isNullOrEmpty(wantedUserId)) {
            wantedUser = userDao.findByPrimaryKey(Integer.parseInt(wantedUserId));
        }


        if(null == wantedUserId) {

           myModel.put("himself",  true  );
           myModel.put("wantedUserId",getLoggedInUser(httpServletRequest).getUserId());

        } else {
             //giris yapan user ile wanted user ayni ise himself diger durumda degil
            myModel.put("wantedUserId",httpServletRequest.getParameter("wantedUserId"));            
            myModel.put("himself", getLoggedInUser(httpServletRequest).getUserId() ==Integer.parseInt(wantedUserId) ? true : false );
        }




        controlUserLikes(httpServletRequest, wantedUser);
        boolean isHeFriend = isHeFriend(getLoggedInUser(httpServletRequest), wantedUser);
        boolean friendshipApprovalWaiting = false;
        if(isHeFriend) {
            FriendsPK friendPK = new FriendsPK();
            friendPK.setUser1Id(getLoggedInUser(httpServletRequest).getUserId());
            friendPK.setUser2Id(Integer.parseInt(wantedUserId));
            Friend friend = friendDao.findByPrimaryKey(friendPK);
            if(null != friend)
                friendshipApprovalWaiting = friend.getApproved() == 0 ? true : false;
        }
        myModel.put("user", wantedUser);
        myModel.put("loggedInUser", getLoggedInUser(httpServletRequest));
        myModel.put("isHeFriend", isHeFriend);
        myModel.put("friendshipApprovalWaiting", friendshipApprovalWaiting);
        return new ModelAndView("profilePage", "model", myModel);
    }

    /**
     * Handles HTTP POST action
     *
     * @param httpServletRequest http request
     * @param httpServletResponse http response
     * @return the result message
     */
    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView handlePost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        Map<String, Object> myModel = Maps.newHashMap();

        String feed = httpServletRequest.getParameter("feed");
        String wantedUserId = httpServletRequest.getParameter("wantedUserId");
        User wantedUser = null;

        if(!Strings.isNullOrEmpty(feed)) {
            compositeDao.addUserFeed(getLoggedInUser(httpServletRequest).getUserId(), Integer.parseInt(wantedUserId), feed);    
        }

        if(!Strings.isNullOrEmpty(wantedUserId)) {
            wantedUser = userDao.findByPrimaryKey(Integer.parseInt(wantedUserId));
        }
        controlUserLikes(httpServletRequest, wantedUser);
        myModel.put("user", wantedUser);
        if(null == wantedUserId) {

           myModel.put("himself",  true  );

        } else {
             //giris yapan user ile wanted user ayni ise himself diger durumda degil
            myModel.put("himself", getLoggedInUser(httpServletRequest).getUserId() ==Integer.parseInt(wantedUserId) ? true : false );
        }
        return new ModelAndView("profilePage", "model", myModel);
    }


    /**
     * Gets the user likes
     *
     * @param httpServletRequest http request
     * @return the likes counts
     */
    private void controlUserLikes(HttpServletRequest httpServletRequest, User wantedUser){
        List<UserAction> actionList = wantedUser.getActionList();
        for (UserAction userAction : actionList) {
            userAction.setLoggedInUserLiked(getLoggedInUser(httpServletRequest).getUserId());
        }
        wantedUser.setActionList(new HashSet<UserAction>(actionList));
    }

    private boolean isHeFriend(User loggedInUser, User wantedUser){
        for (Friend friend : loggedInUser.getFriends()) {
            if(wantedUser.getUserId().intValue() == friend.getUser2Id().intValue()) {
                return true;
            }
        }
        return false;
    }

}
